#include "Sprite.h"
#include "Module.h"
#include "Vector.h"
#include "Utils.h"
#include "Texture.h"

Sprite::Sprite()
{
	renderTime = 0.0f;
}
Sprite::Sprite(float renderTime)
{
	this->renderTime = renderTime;
}

Sprite::Sprite(int x, int y, int width, int height, Texture* texture, float renderTime)
{
	Module* module = new Module(x, y, width, height, texture);
	AddModule(module);
	this->renderTime = renderTime;
}

void Sprite::AddModule(Module* module)
{
	modules.push_back(module);
}

void Sprite::RemoveModule(int index)
{
	int ii = 0;
	int modulesSize = modules.size();
	for(int i = 0; i < modules.size(); i++)
	{
		if(ii=index)
		{
			modules.erase(modules.begin() + i);
			break;
		}
	}
}

void Sprite::Render(int x, int y, float deep)
{
	int modulesSize = modules.size();
	Module* module;
	for(int i = 0; i < modulesSize; i++)
	{
		module = modules[i];
		module->Render(x+module->GetPosInRect()->x,
					  y+module->GetPosInRect()->y,
					  deep);
	}
}

void Sprite::Render(int x, int y, float alpha, float deep)
{
	int modulesSize = modules.size();
	Module* module;
	for(int i = 0; i < modulesSize; i++)
	{
		module = modules[i];
		module->Render(x+module->GetPosInRect()->x,y+module->GetPosInRect()->y,alpha, deep);
	}
}
void Sprite::Render(int x, int y, float xScale, float yScale, float angle, float alpha,float deep)
{
	int modulesSize = modules.size();
	Module* module;
	for(int i = 0; i < modulesSize; i++)
	{
		module = modules[i];
		module->Render(x+module->GetPosInRect()->x,
			y+module->GetPosInRect()->y,
			xScale, yScale, angle, alpha, deep);
	}
}

Sprite::~Sprite()
{
	int modulesSize = modules.size();
	for(int i = 0; i < modulesSize; i++)
	{
		DELETE_SAFELY(modules[i]);
	}
	modules.clear();
}